<template>
  <div>
    <div class="action-bar">
      <!-- <el-button v-if="status == 'created'" type="primary" size="mini" icon="el-icon-plus" @click="add">新建</el-button> -->
      <el-button type="primary" size="mini" icon="el-icon-plus" @click="add">新建</el-button>
      <!-- <el-button type="primary" size="mini" icon="el-icon-back" @click="back">返回</el-button> -->
      <!-- <el-upload
        :headers="headers"
        :action="importExcelurl"
        :before-upload="handleBeforeUpLoad"
        :on-success="handleSuccess"
        :on-error="handleError"
        :show-file-list="false"
      >
        <el-button size="mini" plain icon="el-icon-upload2">导入</el-button>
      </el-upload> -->
    </div>
    <FormDialog title="新建" :show="dialogFormVisible" @on-ok="ok" @on-cancel="cancel">
      <EditForm ref="editForm" />
    </FormDialog>
  </div>
</template>
<script>
import FormDialog from '@neu/biz-core/components/FormDialog'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './editForm'

import {
  importExcelUrl
} from '@neu/dcam-ui/api/inventoryDuty'
import { getToken } from '@neu/biz-core/utils/auth'

export default {
  name: 'InventoryDutyActionBar',
  components: {
    FormDialog,
    EditForm
  },
  props: {
    refresh: {
      required: true,
      type: Function
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      status: this.$route.query.status ? this.$route.query.status : '',
      importExcelurl: importExcelUrl,
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      }
    }
  },
  methods: {
    // 新建用户按钮点击处理
    add() {
      this.dialogFormVisible = true
    },
    // 创建用户对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
        this.$refs.editForm.reset()
      })
    },
    cancel() {
      this.$refs.editForm.reset()
      this.dialogFormVisible = false
    },
    // 返回计划列表
    back() {
      this.$router.push({ name: 'dcam_inventoryPlan' })
    },
    handleBeforeUpLoad(file) {
      if (file.type !== 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
        this.$message({
          showClose: true,
          message: '文件 ' + file.name + ' 格式不正确，请上传.xls,.xlsx文件。',
          type: 'error'
        })
      }
    },
    handleSuccess(response, file, fileList) {
      if (response.status === 'failure') {
        this.$message({
          showClose: true,
          message: response.data,
          type: 'error'
        })
      } else {
        this.$message({
          showClose: true,
          message: '文件上传成功',
          type: 'success'
        })
        this.refresh()
      }
    },
    handleError() { }
  }

}
</script>
